package main.gui.dataobjecteditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.SwingConstants;

import data.EditorObject;
import data.ObjectDatabase;

public class BooleanEditorPanel extends DataAttributePanel implements
		ActionListener {

	private static final long serialVersionUID = -2244922286807093741L;

	private JCheckBox booleanCheckbox;

	public BooleanEditorPanel(String attrName, String objectGUID,
			String fqClassName) {
		super(attrName, objectGUID, fqClassName);
	}

	@Override
	public void buildPanel(Object value) {
		booleanCheckbox = new JCheckBox();
		booleanCheckbox.setBackground(this.getBackground());
		booleanCheckbox.setHorizontalAlignment(SwingConstants.CENTER);
		booleanCheckbox.setSelected((Boolean) value);
		booleanCheckbox.addActionListener(this);
		this.add(booleanCheckbox);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		EditorObject editorObject = ObjectDatabase.getInstance()
				.getEditorObject(fqClassName, objectGUID);
		boolean value = booleanCheckbox.isSelected();
		System.err.println("Value changed to " + value);
		if (editorObject.validateValue(attrName, value)) {
			editorObject.getData().put(attrName, value);
		}
	}
}
